package com.xbongbong.pro.dataset.node;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xbongbong.paas.enums.bi.DataSetNodeTypeEnum;
import com.xbongbong.pro.dataset.pojo.OutputNode;
import com.xbongbong.pro.statistic.pojo.dto.SqlParseDTO;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author gzt
 * @version 1.0
 * @Description -
 * @datetime - 2022/5/23-10:20
 * @since v1.0
 */
@Component
public class OutputNodeStrategy implements NodeStrategy {
    @Override
    public void parseNodeToSql(SqlParseDTO sqlParseDTO) {
        String nodeId = sqlParseDTO.getNodeId();
        JSONObject jsonObject = sqlParseDTO.getJsonObject();
        OutputNode outputNode = jsonObject.toJavaObject(new TypeReference<OutputNode>() {});
        List<String> inputList = outputNode.getInput();
        int size = inputList.size();
        if (size != 1){return;}
        String node = inputList.get(0);
        Map<String, String> sqlNodes = SQL_NODES_THREAD_LOCAL.get();
        String nodeSql = sqlNodes.get(node);
        sqlNodes.put(nodeId,nodeSql);
    }
    
    @Override
    public String getNodeType() {
        return DataSetNodeTypeEnum.OUTPUT.getAlias();
    }

    @Override
    public String getParseSql(String nodeId) {
        Map<String, String> sqlNodes = SQL_NODES_THREAD_LOCAL.get();
        return sqlNodes.get(nodeId);
    }
}
